package com.andy.blog.dal.service.impl;

import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.AlbumDO;
import com.andy.blog.dal.mapper.AlbumMapper;
import com.andy.blog.dal.service.AlbumDalService;
import com.andy.blog.dal.vo.AlbumVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * Created by andy on 2023/10/15 20:54
 */
@Service
public class AlbumDalServiceImpl implements AlbumDalService {

    @Resource
    private AlbumMapper albumMapper;

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<AlbumDO> buildQueryWrapper(AlbumVO albumVO) {
        LambdaQueryWrapper<AlbumDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(albumVO.getAlbumTypeList()), AlbumDO::getAlbumType, albumVO.getAlbumTypeList())
                .orderByDesc(AlbumDO::getId);
        return lambdaQueryWrapper;
    }


    @Override
    public void insertData(AlbumVO albumVO) {
        AlbumDO albumDO = TransformUtils.transform(albumVO, AlbumDO.class);

        albumDO.setCreateBy(SecurityUtils.getUsername());

        int row;
        try {
            row = albumMapper.insert(albumDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }

        if (!Objects.equals(row, 1)) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100);
        }
    }

    @Override
    public IPage<AlbumDO> selectPage(AlbumVO albumVO) {
        LambdaQueryWrapper<AlbumDO> lambdaQueryWrapper = buildQueryWrapper(albumVO);

        IPage<AlbumDO> page = new Page<>(albumVO.getPageNum(), albumVO.getPageSize());

        return albumMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public List<AlbumDO> selectList(AlbumVO albumVO) {
        LambdaQueryWrapper<AlbumDO> lambdaQueryWrapper = buildQueryWrapper(albumVO);

        return albumMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public AlbumDO getById(Long id) {
        return albumMapper.selectById(id);
    }

    @Override
    public void update(AlbumVO albumVO) {
        AlbumDO albumDO = TransformUtils.transform(albumVO, AlbumDO.class);

        albumDO.setUpdateBy(SecurityUtils.getUsername());

        int row;
        try {
            row = albumMapper.updateById(albumDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101, e);
        }

        if (!Objects.equals(row, 1)) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101);
        }
    }

}
